package com.example.hystrix;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import com.example.hystrix.annocation.DoHystrix;
import com.example.hystrix.valve.HystrixValveImpl;
import com.example.hystrix.valve.IValveService;

@Aspect
@Component
public class DoHystrixPoint {
	
	@Pointcut("@annotation(com.example.hystrix.annocation.DoHystrix)")
	public void aopPoint() {
	}
	
	@Around("aopPoint() && @annotation(doGovern)")
	public Object doRouter(ProceedingJoinPoint jp, DoHystrix doGovern) throws Throwable {
		IValveService valveService = new HystrixValveImpl();
		return valveService.access(jp, getMethod(jp), doGovern, jp.getArgs());
	}
	
	private Method getMethod(JoinPoint jp) throws NoSuchMethodException {
		Signature sig = jp.getSignature();
		MethodSignature methodSignature = (MethodSignature) sig;
		return jp.getTarget().getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
	}
	
}
